type anyObject = {
  [key: string]: any;
};

export function getType(val: any) {
  return Object.prototype.toString.call(val).slice(8, -1);
}

export function isDef<V>(val: V): val is NonNullable<V> {
  return val !== undefined && val !== null;
}

// 下划线转驼峰
export function toCamelCase(name: string): string {
  return name.replace(/\_(\w)/g, function (all, letter) {
    return letter.toUpperCase();
  });
}

// 驼峰转下划线
export function toUnderLineCase(name: string): string {
  return name.replace(/([A-Z])/g, '_$1').toLowerCase();
}

// 将对象的所有属性，下划线转驼峰
export function toCamelCaseWithObj(obj: anyObject): anyObject {
  const tempObj = {};
  for (const key in obj) {
    tempObj[toCamelCase(key)] = obj[key];

    if (getType(obj[key]) === 'Object') {
      tempObj[toCamelCase(key)] = toCamelCaseWithObj(obj[key]);
    }
  }
  return tempObj;
}

// 将对象的所有属性，驼峰转下划线
export function toUnderLineCaseWithObj(obj: anyObject): anyObject {
  const tempObj = {};
  for (const key in obj) {
    tempObj[toUnderLineCase(key)] = obj[key];

    if (getType(obj[key]) === 'Object') {
      tempObj[toUnderLineCase(key)] = toUnderLineCaseWithObj(obj[key]);
    }
  }
  return tempObj;
}
